<template>
  <div class="content">
    <div class="login">
      <h1 style="margin-bottom: 18px; position: relative; top: 9px">
        廊坊市公安局校灵通服务
      </h1>
      <div
        style="
          width: 70px;
          height: 4px;
          background-color: #126bff;
          margin: 10px auto;
          border-radius: 5px;
          position: relative;
          top: 8px;
        "
      ></div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        style="text-align: center; margin-top: 30px"
        @keyup.enter.native="submitForm('ruleForm')"
      >
        <el-form-item prop="ex_mobile">
          <p class="tip">账号</p>
          <el-input
            style="width: 415px; height: 48px"
            v-model="ruleForm.ex_mobile"
            placeholder="请输入账号"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="ex_pwd">
          <p class="tip">密码</p>
          <el-input
            show-password
            style="width: 415px; height: 48px"
            type="password"
            placeholder="请输入密码"
            v-model="ruleForm.ex_pwd"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            style="
              width: 410px;
              margin-top: 20px;
              border-radius: 8px;
              background-color: #126bff;
              color: #ffffff;
              height: 50px;
              font-weight: bold;
              font-size: 18px;
              position: relative;
              bottom: 6px;
            "
            ref="button"
            @click="submitForm('ruleForm')"
            >登 录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { login } from "../../api/inedx";
import { searchAdminMenuPage } from "@/api/police";
export default {
  name: "APP",
  components: {},
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else {
        if (this.ruleForm.ex_mobile !== "") {
          this.$refs.ruleForm.validateField("id");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        ex_pwd: "",
        ex_mobile: "",
        // code: "", 123456   xiaolingtong
      },
      rules: {
        ex_pwd: [{ validator: validatePass2, trigger: "blur" }],
        ex_mobile: [{ validator: validatePass, trigger: "blur" }],
        // code: [{ validator: checkCode, trigger: "blur" }],
      },
    };
  },
  watch: {
    $route(to) {
      // console.log(to, from);
      // localStorage.setItem("activeIndex", to.fullPath);
      // this.activeIndex = to.fullPath;
    },
  },
  created() {},
  mounted() {
    this.initip();
  },

  methods: {
    initip() {
      this.$http.get("https://api.ipify.org/?format=json").then((res) => {
        // res = JSON.parse(res);
        console.log(res.data.ip);
        window.localStorage.setItem("ip", res.data.ip);
        // this.Ip = res.data.ip;
        // console.log("IP地址为", res.data.ip);

        // this.Ip = data.ip;
        // console.log("IP地址为", data.ip);
      });
    },
    submitForm(formName) {
      var that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // ========
          // 这里去写账号密码是否加密 需要跟后端去定一下加密解密方式
          // ==========
          login(this.ruleForm).then((res) => {
            console.log(res);
            // console.log(res.data.menu[1].router);
            if (res.code == 200) {
              that.$cookies.set("token", res.data.token); // 设置过期时间未60s

              if (res.data.user.role_id == 68) {
                // 只有学校账号登录到大屏
                that.$router.push("dataScreen");
              } else {
                that.$router.push(res.data.menu[1].router); // 管理员
              }
              // that.$router.push("/layout"); // 公安 /layout/imgwrite
              // that.$router.push("/dataScreen"); // 公安 /layout/imgwrite
              // that.$router.push("/PublicSecurity"); // 学校互联网端 /layout/imgwrite
              localStorage.setItem("MenuList", JSON.stringify(res.data.menu));
              localStorage.setItem("userInfo", JSON.stringify(res.data));
              localStorage.setItem(
                "activeIndex",
                JSON.stringify(res.data.menu[1].router)
              );
            } else if (res.code == 10021) {
              alert(res.message);
            } else if (res.code == 400) {
              alert(res.message);
            } else if (res.code == 10017) {
              alert(res.message);
            } else {
              alert(res.message);
            }
          });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<!-- sass-loader -->
<style scss="sass"  scoped >
.content {
  height: 100vh;
  background: url(../../static/img/104.png) no-repeat;
  background-size: cover;
}
.login {
  background-color: white;
  width: 491px;
  height: 450px;
  padding: 34px;
  padding-left: 20px;
  border-radius: 20px;
  box-sizing: border-box;
  /* border: 1px red solid; */
  margin: 0 auto;
  position: absolute;
  top: 39%;
  left: 25%;
  transform: translate(-25%, -25%);
}
.tip {
  font-weight: bold;
  text-align: left;
  height: 30px;
  line-height: 20px;
  /* padding-left: 20px; */
}
.login ::v-deep .el-form-item__content {
  margin: 0 !important;
  margin-left: 0 !important;
  height: 80px;
  padding-left: 22px;
}
.el-form-item {
  color: #13213b;
}
.el-input {
  height: 30px !important;
}
.el-input__inner {
  border: none !important;
  height: 48px !important;
  background-color: #f7f7f9 !important;
}
::deep.el-form-item__error {
  left: none;
}
</style>
